﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yule8xCommon.Applib;
using Yule8xCommon.Db;
using Yule8xCommon.Typelib;
using Yule8xModel;
using Yule8xWechat.Applib;

namespace Yule8xWechat.Agent
{
    public partial class ChildAgent : System.Web.UI.Page
    {
        protected WxUserInfoResult wxUser;
        protected System.Data.DataRow dtUser;
        protected string todayincome, totalincome; // 今日收入, 累计收入
        protected string yinpaicount, tongpaicount; // 银牌代理数，铜牌代理数
        protected int user_id;
        protected void Page_Load(object sender, EventArgs e)
        {
            user_id = RequestUtil.GetInteger("user_id");
            if (RuntimeDataUtil.CheckDebugModel() && user_id > 0)
            {
                wxUser = UserModels.GetWxUserInfo(user_id);
                if (null == wxUser)
                {
                    ResponseUtil.WriteEnd("user_id无效!");
                }
            }
            else
            {
                wxUser = LoginPageBase.GetLoginInfo();
                if (null == wxUser)
                {
                    Response.Redirect(WechatUrls.GetUserInfoUrl("agent"), true);
                }
            }
            /*
            object loginData = Session["login"];
            if (null != loginData)
            {
                wxUser = loginData as WxUserInfoResult;
            }
            if (null == loginData || null == wxUser)
            {
                Response.Redirect(WechatUrls.GetUserInfoUrl("agent"), true);
            }
            */
            string sqlstmt;
            // 累计收入: 查询银牌和铜牌代理统计报表的sum(today_amount)字段之和
            sqlstmt = string.Format("select sum(total_amount) from t_agent1_report where user_id={0}", wxUser.user_id);
            double n1 = DbFactory.Init(AppInfo.conn_yule8x_agent).ExecuteSum<double>(sqlstmt);
            sqlstmt = string.Format("select sum(total_amount) from t_agent2_report where user_id={0}", wxUser.user_id);
            double n2 = DbFactory.Instance.ExecuteSum<double>(sqlstmt);
            totalincome = (n1 + n2).ToString();
            // 今日收入
            sqlstmt = string.Format("select sum(today_amount) from t_agent1_report where user_id={0}", wxUser.user_id);
            n1 = DbFactory.Init(AppInfo.conn_yule8x_agent).ExecuteSum<double>(sqlstmt);
            sqlstmt = string.Format("select sum(today_amount) from t_agent2_report where user_id={0}", wxUser.user_id);
            n2 = DbFactory.Instance.ExecuteSum<double>(sqlstmt);
            todayincome = (n1 + n2).ToString();

            // 银牌代理数量: 查询用户表count(0)字段，条件是父1级=我或父2级=我，并且代理类型=银牌
            sqlstmt = string.Format("select count(0) from t_user where parent1_uid={0} and is_agent=1", wxUser.user_id);
            yinpaicount = DbFactory.Instance.GetValue(sqlstmt).ToString();
            // 铜牌代理数量: 
            sqlstmt = string.Format("select count(0) from t_user where parent2_uid={0} and is_agent=1", wxUser.user_id);
            tongpaicount = DbFactory.Instance.GetValue(sqlstmt).ToString();

            // 更新下级代理统计
            //DbFactory.Init(AppInfo.conn_yule8x_agent).ExecuteNonQuery(string.Format("update t_user set child1_agent_count=(select count(0) from t_user where parent1_uid={0} and is_agent=1), child2_agent_count=(select count(0) from t_user where parent2_uid={0} and is_agent=1) where id={0}", wxUser.user_id));
            // 获取用户更多信息
            //dtUser = DbFactory.Init(AppInfo.conn_yule8x_agent).ExecuteDataTable(string.Format("select top 1 * from t_user where id={0}", wxUser.user_id)).Rows[0];
        }
    }
}